package Main;

import url.DBUtil;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.ArrayList;

public class MainFrame extends JFrame {
    private JTable table;
    private String[] columnNames = {"编号", "名称" , "容纳人数" , "预约人数", "位置"};
    public MainFrame() throws SQLException {

        Font f = new Font("宋体", Font.PLAIN, 25);
        // 窗口大小
        setSize(1200, 800);
        // 窗口可视化
        setVisible(true);
        // 窗口标题
        setTitle("场地管理系统");
        // 窗口初始化位置
        setLocationRelativeTo(null);
        // 窗口大小不可改变
        setResizable(false);
        // 窗口的关闭事件
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout());
        add(contentPane);

        JPanel jPanel4 = new JPanel();
        jPanel4.setPreferredSize(new Dimension(1200, 60));
        jPanel4.setLayout(new BorderLayout());
        // 设置 JPanel 的背景颜色为紫色
        jPanel4.setBackground(new Color(0, 94, 128, 232)); // 使用RGB值表示
        JLabel textLabel = new JLabel("场地管理系统");
        textLabel.setForeground(Color.WHITE); // 设置文本颜色为白色
        textLabel.setFont(f);
        textLabel.setHorizontalAlignment(SwingConstants.CENTER);
        jPanel4.add(textLabel, BorderLayout.CENTER);
        contentPane.add(jPanel4, BorderLayout.NORTH);


        JPanel jPanel3 = new JPanel();
        jPanel3.setPreferredSize(new Dimension(60, 800));
        jPanel3.setLayout(new BoxLayout(jPanel3, BoxLayout.Y_AXIS));
        jPanel3.setBackground(Color.pink);

        JLabel label = new JLabel();
        ImageIcon icon = new ImageIcon("src/img/3.png");
        Image img = icon.getImage();
        Image resizedImg = img.getScaledInstance(40, 40, Image.SCALE_SMOOTH);
        icon = new ImageIcon(resizedImg);
        label.setIcon(icon);
        label.setBorder(BorderFactory.createEmptyBorder(30, 10, 15, 5));
        jPanel3.add(label);

        // 添加2个子菜单
        icon = new ImageIcon("src/img/4.png");
        img = icon.getImage();
        resizedImg = img.getScaledInstance(40, 40, Image.SCALE_SMOOTH);
        icon = new ImageIcon(resizedImg);
        JLabel subLabel = new JLabel();
        subLabel.setIcon(icon);
        subLabel.setPreferredSize(new Dimension(40, 40)); // 设置子菜单的大小
        subLabel.setBorder(BorderFactory.createEmptyBorder(30, 10, 15, 5));
        jPanel3.add(subLabel);

        addImage(jPanel3);
        contentPane.add(jPanel3, BorderLayout.WEST);


        JPanel jPanel0 = new JPanel();
        jPanel0.setPreferredSize(new Dimension(700, 600));
        jPanel0.setBackground(Color.blue);
        jPanel0.setLayout(new BorderLayout());
        contentPane.add(jPanel0, BorderLayout.CENTER);


        JPanel jPanel01 = new JPanel();
        // 设置 jPanel05 面板的布局为流式布局，水平居中对齐
        jPanel01.setLayout(new FlowLayout(FlowLayout.CENTER));

        // 创建两个文本框和一个按钮
        JLabel jb1 = new JLabel("编号:");
        jb1.setFont(f);
        JLabel jb2 = new JLabel("名称:");
        jb2.setFont(f);
        JTextField textField1 = new JTextField(10);
        textField1.setPreferredSize(new Dimension(200, 25));
        textField1.setFont(new Font("宋体", Font.PLAIN, 20));

        JTextField textField2 = new JTextField(10);
        textField2.setPreferredSize(new Dimension(200, 25));
        textField2.setFont(new Font("宋体", Font.PLAIN, 20));
        JButton button = new JButton("查询");
        button.setFont(new Font("宋体", Font.PLAIN, 16));
        jPanel01.add(jb1);
        jPanel01.add(textField1);
        jPanel01.add(jb2);
        jPanel01.add(textField2);
        jPanel01.add(button);
        jPanel0.add(jPanel01, BorderLayout.NORTH);

        JPanel jPanel02 = new JPanel();
        // 设置 jPanel05 面板的布局为流式布局，水平居中对齐
        jPanel01.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton jbt1 = new JButton("添加");
        jbt1.setFont(f);
        JButton jbt2 = new JButton("删除");
        jbt2.setFont(f);
        JButton jbt3 = new JButton("预约");
        jbt3.setFont(f);
        jPanel02.add(jbt1);
        jPanel02.add(jbt2);
        jPanel02.add(jbt3);
        jPanel0.add(jPanel02,BorderLayout.SOUTH);


        DBUtil.initst();
        ArrayList<venue> list = new ArrayList<>();
        String sql = "select * from venue";
        DBUtil.select(sql);
        while(DBUtil.rs.next()){
            venue v = new venue();
            v.setNumber(DBUtil.rs.getString(1));
            v.setName(DBUtil.rs.getString(2));
            v.setSize(DBUtil.rs.getInt(3));
            v.setCount(DBUtil.rs.getInt(4));
            v.setLocation(DBUtil.rs.getString(5));
            list.add(v);
        }
        DBUtil.closeDB();

        // 创建数据数组
        Object [][] data=new Object[list.size()][columnNames.length];
        for(int i = 0; i<list.size(); i++){
            venue v = list.get(i);
            data[i][0] = v.getNumber();
            data[i][1] = v.getName();
            data[i][2] = v.getSize();
            data[i][3] = v.getCount();
            data[i][4] = v.getLocation();
        }
        // 创建DefaultTableModel并设置列名和数据
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model);
        table.setEnabled(false);
        table.setFont(new Font("宋体", Font.PLAIN, 20));
        table.setBorder(null);
        // 设置行高
        table.setRowHeight(35);
        JTableHeader header = table.getTableHeader();
        // 设置列名的大小
        header.setFont(new Font("宋体", Font.BOLD, 20));
        //设置列表头不可别用户重新拖动排列
        table.getTableHeader().setReorderingAllowed(false);
        // 添加JTable到JScrollPane
        JScrollPane scrollPane = new JScrollPane(table);
        jPanel0.add(scrollPane,BorderLayout.CENTER);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String number = textField1.getText();
                String name = textField2.getText();
                String sql;
                if(name.isEmpty()&& number.isEmpty()){
                    sql = "select * from venue";
                }else if(!name.isEmpty()&&!number.isEmpty()){
                    sql = "select * from venue where number like '%" + number + "%' and name like '%" + name + "%'";

                }else if(!number.isEmpty()){
                    sql = "select * from venue where number like '%" + number + "%'";
                }else{
                    sql = "select * from venue where name like '%" + name + "%'";
                }
                ArrayList<venue> list = new ArrayList<>();
                try {
                    list = (ArrayList<venue>) ShowData.getDbdata(sql);
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
                // 创建数据数组
                Object[][] data = new Object[list.size()][columnNames.length];
                for (int i = 0; i < list.size(); i++) {
                    venue v = list.get(i);
                    data[i][0] = v.getNumber();
                    data[i][1] = v.getName();
                    data[i][2] = v.getSize();
                    data[i][3] = v.getCount();
                    data[i][4] = v.getLocation();
                }

                // 更新表格的数据模型
                DefaultTableModel model = new DefaultTableModel(data, columnNames);
                table.setModel(model);

            }
        });
        jbt1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new InsertFrame();
            }
        });
        jbt2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new DeleteFrame();
            }
        });
        jbt3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new OrderFrame();
            }
        });
    }

    public void addImage(JPanel panel) {
        ImageIcon icon = new ImageIcon("src/img/5.png");
        Image img = icon.getImage();
        Image resizedImg = img.getScaledInstance(40, 40, Image.SCALE_SMOOTH);
        icon = new ImageIcon(resizedImg);

        JLabel subLabel = new JLabel();
        subLabel.setIcon(icon);
        subLabel.setPreferredSize(new Dimension(40, 40)); // 设置子菜单的大小
        subLabel.setBorder(BorderFactory.createEmptyBorder(30, 10, 15, 5));
        // 添加鼠标监听器
        subLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                try {
                    new AppoimentFrame();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        panel.add(subLabel);
    }




}
